$(function(){
	$(".star-togger").click(function(){
		var obj=$(".star-icon");
		var i=0;			
		var manystar=[];
		obj.each(function(){
			if($(this).children("i").hasClass("icon-collection_fill")){
				i++;
			}else{
				$(this).children("i").addClass("icon-collection_fill").removeClass("icon-collection");			
				manystar.push($(this).attr("dataid"));
			}		
		})			
		if(i==obj.length){		
			obj.children("i").removeClass("icon-collection_fill").addClass("icon-collection");
			obj.each(function(){
				manystar.push($(this).attr("dataid"));
			})
		}else{
		}
	})
	/**
	 * 提交通讯录表单
	 */
	$("#addressForm").form({
		url: "/address/keepaddress",
		onSubmit:function(){
			//验证邮件
			var emailPatt=/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/;
			if($("input[name=email]").val()!="" && !emailPatt.test($("input[name=email]").val())){
				layer.tips('<span style="color:#aa0000">'+'邮箱格式不正确'+'</span>', $("input[name=email]"), {
					tips : [ 2, 'rgb(236, 228, 221)' ]
				});
				return false;
			}
		},
		success:function(data){
			if(data){
				layer.msg('保存成功',{shift: -1,time:1000}, function(){
					refreshPanel(getUrlPrefix("/address"),"通讯录");
				});
			}else{
				layer.msg('保存失败',{shift: -1,time:1000});
			}
	    }
	});
	$(".address-save").click(function(){
		$("#addressForm").submit();
	})
	//选择不同类型通讯录
	$(".address-status").on("click",".address-menu",function(){
		$(this).siblings().removeClass("border-left");
		if($(this).hasClass("border-left")){
			$(this).addClass("border-left");
		}else{
			$(this).removeClass("border-left");
		}
		var dirid=$(".border-right").attr("dirid");
		if(dirid==undefined){
			dirid="";
		}
		refreshPanel(getUrlPrefix("/address/addressstatus?statusid="+$(this).attr("status")+"&&typeid="+dirid+""),"通讯录");
	})
	
	$(".address-table").on("click",".btn-update",function(){
		refreshPanel(getUrlPrefix("/address/editaddress?addressid="+$(this).attr("addressid")+""),"编辑联系人");
	})
	
	//不同类别
	$(".type-ul").on("click",".address-type",function(){
		$(this).siblings().removeClass("border-right");
	
		$(this).addClass("border-right");
		
		var status=$(".border-left").attr("status");
		if(status==undefined){
			status="";
		}
		refreshPanel(getUrlPrefix("/address/addresstypedata?typeid="+$(this).attr("dirid")+"&&statusid="+status+""), "通讯录");
	})
	$(".recently").on("click",function(){
		refreshPanel(getUrlPrefix("/address"), "通讯录");
		
	})
	$(".address-btn-cancel").click(function(){
		var content="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>正在编辑联系人,确定要退出么？</span>\<\/div>";	 
		  layer.open({ 
			  type: 1,		
			  skin: 'demo-class',
			  skin: 'yourclass',
			  offset: '100px',
			  title :'提示',
			  area: ["300px","200px"], //宽高
			  content: content,
			  closeBtn: 0,
			  btn:['确定','关闭'],
			  btnAlign: 'c', 
			  yes:function(index, layero){
				  refreshPanel(getUrlPrefix("/address"), "通讯录");
				  layer.close(index);	
			  },
			  btn2: function(index, layero){
				layer.close(index);		   
			  }
			})

	})
	//选择共享通讯录触发事件
	$(".addaddress-type").change(function(){
		if($(this).val()==2){
			$(".share-td").show();
		}else{
			$(".share-td").hide();
		}
	})
	$(".img-div").click(function(){
		$(".file-input").trigger("click");
	})
		/**
		 * 添加新的分类
		 * 
		 * @returns
		 */
		$(".new-type").click(function(){
			var value=$(".new-type-input").val();
			if(value==""){
				var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>类型名称不能为空！！！</span>\<\/div>";	 
				promptbox(obj);	
				return;
			}
			 $.ajax({
				  type:"post",
				  url:"/address/newtype",
				  cache:false,
				  async:false,
				  data:{"value":value},
				  datatype:"json",
				  success:function(data){
					  $(".type-ul").find("li:first").nextAll().remove();
					  $.each(data,function(i,item){
						newtype(item);
					  })
				  }
			  })
			  $(".new-type-input").val("");
		})
		//删除
		$(".address-table").on("click",".btn-delete",function(){
			var value=$(this).attr("addressid");
			var path=$(this).attr("path");
			if($(".border-left").attr("status")==4){
					layer.confirm('删除后不可恢复，确认删除？',{closeBtn: 0},function(index){
					$.ajax({
						  type:"post",
						  url:"/address/deleteshareuser",
						  cache:false,
						  async:false,
						  data:{"value":value},
						  success:function(data){}
					 })
					 refreshPanel(getUrlPrefix(""+path+""), "通讯录");
					 layer.close(index);
					})
				}else if($(".border-left").attr("status")==2){
					layer.confirm('删除后分享用户不能共享这条记录，确认删除？',{closeBtn: 0},function(index){
					$.ajax({
						  type:"post",
						  url:"/address/deletegonxiang",
						  cache:false,
						  async:false,
						  data:{"value":value},
						  success:function(data){}
					 })
					 refreshPanel(getUrlPrefix(""+path+""), "通讯录");
					 layer.close(index);
					})
				}else{
				layer.confirm('删除后不可恢复，确认删除？',{closeBtn: 0},function(index){
					$.ajax({
						  type:"post",
						  url:"/address/deleteaddress",
						  cache:false,
						  async:false,
						  data:{"value":value},
						  success:function(data){}
					 })
					refreshPanel(getUrlPrefix(""+path+""), "通讯录");
					layer.close(index);
					})
				}
		})
		//添加联系人
	$(".fancybox").click(function() {
		layer.open({
			type : 1,
			skin : 'demo-class',
			skin : 'yourclass',
			offset : '100px',
			title : '联系人',
			area : [ '804px', '492px' ], // 宽高
			content : $(".contacts-tables"),
			closeBtn : 0,
			btn : [ '确定', '取消' ],
			btnAlign : 'c',
			yes : function(index, layero) {
				$(".getmain-user").val("");
				$(".userid-input").val("");
				var obj=$(".icheckbox_flat-green-checked");
				obj.each(function(){				
					$(".getmain-user").val($(".getmain-user").val()+"、"+$(this).attr("name"));
					$(".userid-input").val($(".userid-input").val()+"、"+$(this).attr("data"));
				})
					var str=$(".getmain-user").val();
					var newstr=str.substr(1,str.length);				
					var idstr=$(".userid-input").val();
					var newidstr=idstr.substr(1,idstr.length);
					
					$(".getmain-user").val(" "+newstr);
					$(".userid-input").val(newidstr);
					layer.close(index);
			},
			btn2 : function(index, layero) {
				layer.close(index);
			}
		});
	})
	//查找指定人
	$(".search-btn-shareuser").click(function(){
		var inputval=$(".search-btn-shareuser-input").val();
		$(".contacts-tables tbody").html("");
		if(inputval==""){	
			newalluser();
			return;
		}
		 $.ajax({
			  type:"post",
			  url:"/file/searchshareuser",
			  cache:false,
			  async:false,
			  data:{"inputval":inputval},
			  datatype:"json",
			  success:function(data){
			  $.each(data,function(i,item){
				  newtable(item);		  
				})
			}
		  })
	})
		$(".btn-delete-many").click(function(){
			if($(".border-left").attr("status")==3||$(".border-left").attr("status")==4){
				var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>无权限删除！！！</span>\<\/div>";	 
				promptbox(obj);	
				return;
			}
			var addressid=[];
			var obj=$(".icheckbox_flat-green-checked");
			var path=$(this).attr("path");
			obj.each(function(){
				addressid.push($(this).attr("dataid"));
			})
			layer.confirm('删除后不可恢复，确认删除？',{closeBtn: 0},function(index){
			$.ajax({
				  type:"post",
				  url:"/address/deletemanyaddress",
				  cache:false,
				  traditional:true,
				  async:false,
				  data:{"addressid":addressid},
				  success:function(data){}
			})
			layer.close(index);
			refreshPanel(getUrlPrefix(""+path+""), "通讯录");
			})
		})
		//查看
		$(".address-table").on("click",".btn-look",function(){
			var addressid=$(this).attr("addressid");
			if($(".border-left").attr("status")==3){
				refreshPanel(getUrlPrefix("/address/viewuser?addressid="+addressid+""), "查看联系人");
			}else{
				refreshPanel(getUrlPrefix("/address/viewAddress?addressid="+addressid+""), "查看联系人");
			}
		})
		$(".search-btn").click(function(){
			var value=$(".search-input").val();
			if(value==""){
				var obj="\<\div style='width: 210px;height: auto; margin: 13% auto; text-align:center;'><span style='font-size:14px'>查询不能为空！！！</span>\<\/div>";	 
				promptbox(obj);	
				return;
			}
			refreshPanel(getUrlPrefix("/address?value="+value+""),"通讯录");
		})
		//全选
		$(".checkbox-togger").click(function(e){
				e.preventDefault();
				var num=0;
				var obj=$(".icheckbox");
				obj.each(function(){				
					if($(this).attr("aria-checked")=="false"){
						$(".icheckbox").addClass("icheckbox_flat-green-checked");
						$(".icheckbox").removeClass("icheckbox_flat-green");
						$(".icheckbox").attr("aria-checked","true");
					}else{
						num++;
					}		
				})
				if(num==obj.length){
					$(".icheckbox").addClass("icheckbox_flat-green");
					$(".icheckbox").removeClass("icheckbox_flat-green-checked");
					$(".icheckbox").attr("aria-checked","false");
				};
			});
	//删除类型
	$(".dir-btn-delete").click(function(e){
		 e.stopPropagation();
	})
	/*$(".type-ul").on("mouseover",".address-type",function(){
		$(this).find(".dir-btn-delete").css("display","inline-block");
	})
	$(".type-ul").on("mouseleave",".address-type",function(){
		$(this).find(".dir-btn-delete").css("display","none");
	})*/
	$(".address-type").bind("contextmenu",function(){
			return false;
	})
	$(".type-ul").on("mousedown",".address-type",function(e){
		e.stopPropagation();
		 var x=e.pageX || e.clientX + document.body.scroolLeft;
		  var y=e.pageY || e.clientY + document.boyd.scrollTop;
		 if(3 == e.which){ 
			 if($(".main-left").width()>50){
				  $(".garbager-option").css("left",x-210+"px");
				  $(".garbager-option").css("top",y-40+"px");
			  }else{
				  $(".garbager-option").css("left",x-45+"px");
				  $(".garbager-option").css("top",y-40+"px");
			  }		  			  
			  $(".garbager-option").css("display","block");			
		 }
		 var dirid=$(this).attr('dirid');
		 $(".addressdir-rename").attr("dirid",dirid);
		 $(".addressdir-delete").attr("dirid",dirid);
		 $(document).one("click",function(){
			$(".garbager-option").css("display","none");	
		});	
	})
	//重命名
	$(".addressdir-rename").click(function(){
		var obj=$(".address-type");
		var dirid=$(this).attr('dirid');
		obj.each(function(){
			if($(this).attr("dirid")==dirid){
				$(this).children(".dirname").css("display","none");
				$(this).children(".dirname-input").val($(this).children(".dirname").text());
				$(this).children(".dirname-input").css("display","inline-block");
				$(this).children(".dirname-input").focus();
			}
		})
	})
	//删除
	 $(".addressdir-delete").click(function(){
		 var dirid=$(this).attr('dirid');
		 $.ajax({
			  type:"post",
			  url:"/address/deleteajax",
			  cache:false,
			  async:false,
			  data:{"dirid":dirid},
			  datatype:"json",
			  success:function(data){
				  $(".type-ul").find("li:first").nextAll().remove();
				  $.each(data,function(i,item){
					newtype(item);
				  })
			  }
		  })
	})
	 
	$(".type-ul").on("click",".dirname-input",function(e){
		e.stopPropagation();
	})
	$(".type-ul").on("blur",".dirname-input",function(e){
		$(this).css('display',"none");
		$(this).parent().find(".dirname").text($(this).val());
		$(this).parent().find(".dirname").css('display',"inline-block");
		var dirid=$(this).parent().attr("dirid");
		$.ajax({
			  type:"post",
			  url:"/address/updatetypename",
			  cache:false,
			  async:false,
			  data:{"dirid":dirid,"value":$(this).val()},
			  success:function(data){}
		})
		$(this).val("");
	})
	
	//滚动条移动触发事件
	$(".main-center").scroll(function(){
		$(".garbager-option").css("display","none");
	})	
})
// 展开关闭点击事件
function jian(){
	if($(".yunpan").hasClass("s")){		
		$(".yunpan").slideDown();		
		$("#one").attr("class","iconfont icon-jian");
		$(".yunpan").removeClass("s");
	}else{			
		$(".yunpan").slideUp();
		$(".yunpan").addClass("s");
		$("#one").attr("class","iconfont icon-jia");
	}
};

// 展开关闭点击事件
function jian2(e){
	if($(".leixing").hasClass("s")){
		$(".leixing").slideDown();
		$("#two").attr("class","iconfont icon-jian");
		$(".leixing").removeClass("s");
	}else{
		$(".leixing").slideUp();
		$(".leixing").addClass("s");
		$("#two").attr("class","iconfont icon-jia");
	}
};
//邮件选中
function select(e){
	if($(e).hasClass("icheckbox_flat-green-checked")){
		$(e).removeClass("icheckbox_flat-green-checked");
	}else{
		$(e).addClass("icheckbox_flat-green-checked")
	}	
}
//提示框
function promptbox(content){
	  layer.open({ 
		  type: 1,		
		  skin: 'demo-class',
		  skin: 'yourclass',
		  offset: '100px',
		  title :'提示',
		  area: ["300px","200px"], //宽高
		  content: content,
		  closeBtn: 0,
		  btn:['关闭'],
		  btnAlign: 'c', 
		  btn1: function(index, layero){
			layer.close(index);
		    //按钮【按钮二】的回调		    
		    //return false 开启该代码可禁止点击该按钮关闭
		  }
		})
}

/**
 * 用流读取图片
 * @param e
 * @returns
 */
function showPic(e){
	//先判断格式正不正确
	var pattern = /(\.*.jpg$)|(\.*.png$)|(\.*.jpeg$)|(\.*.gif$)|(\.*.bmp$)/;
	if (!pattern.test($(e).val().toLowerCase())) {
		layer.alert("图片格式不正确");
		return;
	}
	console.log(e.files[0]);
	if (getObjectURL(e.files[0])) {
		$("#head").attr("src", getObjectURL(e.files[0]));//预览图片
		$("input[name=photo]").val(e.files[0].name);
	}
}

/**
 * 建立url
 * @param file
 * @returns
 */
function getObjectURL(file) {
	var url = null;
	if (window.createObjectURL != undefined) { // basic
		url = window.createObjectURL(file);
	} else if (window.URL != undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file);
	} else if (window.webkitURL != undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file);
	}
	return url;
}
//ajax生成通讯录类型
function newtype(item){
	  var li=$("<li></li>");
	  var a1=$(" <a><i class='iconfont icon-circle1'></i><span>"+item.name+"</span></a>")				  
	  var a2=$("<a class='dir-btn-delete' dirid='"+item.id+"'' style='margin-left: 28px;display: none;color:#a29d9d'><i class='iconfont icon-trash'></i></a>");
	  li.append(a1);
	  li.append(a2);
	  $(".type-ul").append(li);
}